from myapp.models import UserModel, OperationLogModel


def create_log(request, name):

    try:
        print('==============', request.method, request.path[7:])
        # 写入操作模型类
        user = request.session.get('user')
        user_id = user.get('user_id')
        user = UserModel.objects.filter(id=user_id).first()
        operation = '%s添加了一个%s' % (user.username, name)
        OperationLogModel.objects.create(user_id=user_id, operation=operation, router=request.path[7:])
    except Exception as e:
        print('添加操作日志的报错信息是', e)


def update_log(request, name):
    try:
        print('==============', request.method, request.path[7:])
        # 写入操作模型类
        user = request.session.get('user')
        user_id = user.get('user_id')
        user = UserModel.objects.filter(id=user_id).first()
        operation = '%s修改了一个%s' % (user.username, name)
        OperationLogModel.objects.create(user_id=user_id, operation=operation, router=request.path[7:])
    except Exception as e:
        print('添加操作日志的报错信息是', e)